import { ShareDefine } from "../../../Game/Scripts/Config/ShareDefine";
import NetManager from "../Net/NetManager";
import NetWork from "../Net/NetWork";


const {ccclass, property} = cc._decorator;

@ccclass
export default class ClientManager extends cc.Component {
    public static Instance: ClientManager = null;
    lastHearTime: number;
	isWriteLog: any;
	logSecond: any;
	clientState: any;
	reConnectTip: any;
	bStartReConnect: any;
	lastReConnectTime: number;
	reConnectUseTime: any;
	reConnectLable: any;
	isFirstLogin: boolean;

    onLoad() {
        if (ClientManager.Instance !== null) {
            this.destroy();
            return;
        }
        ClientManager.Instance = this;
    }

	UpdateHearTime () {
		this.lastHearTime = new Date().getTime();
	}
	StartTime () {
		this.schedule(this.OnTimer, 1);
	}
	StopTimer () {
		this.unschedule(this.OnTimer);
	}

    	//100毫秒回掉
	OnTimer (passSecond) {
		try {
			if (this.isWriteLog) {
				this.logSecond += passSecond;
				//500毫秒输出一次log
				if (this.logSecond >= 0.5) {
					this.LogManager.OutPutLog();
					this.logSecond = 0
				}
			}
			if (this.clientState < ShareDefine.State_WaitLogin) {
				console.log("当前游戏在 clientState:State_WaitLogin");
				return
			}
			//判断下服务器是否连上，如果连上关闭tip
			if (NetWork.Instance.isConnectIng && this.reConnectTip.active) {
				this.CloseReConnectTip();
			}
			app.SceneManager().OnTimer(passSecond);

			//完整登录才能调用
			if (this.clientState == ShareDefine.State_Logined) {
				let curTime = new Date().getTime();

				if (this.lastHearTime && curTime > this.lastHearTime + 23000) {
					console.log('NetWork Disconnect By Heart');
					this.lastHearTime = 0;
					if (NetWork.Instance.isConnectIng)//ios的可能websoket不会主动断开
						NetManager.Instance.Disconnect();

					if (!this.bStartReConnect) {
						this.StartReConnect();
						NetWork.Instance.ReConnectByTipSureBtn();
					}
				}
				if (this.bStartReConnect) {
					if (this.lastReConnectTime && curTime > this.lastReConnectTime + 1000) {
						this.lastReConnectTime = curTime;
						this.reConnectUseTime++;
						this.reConnectLable.string = '连接失败,重新加载中(' + this.reConnectUseTime + '秒)' + '...';
					}
				}
			}
		} catch (error) {
			console.log("OnTimer:%s", error.stack);
		}
	}


    isInHall () {
		let curRunGame = cc.sys.localStorage.getItem("curRunGame");
		// console.log("当前本地保存的游戏在"+curRunGame);
		if (curRunGame != "hall") {
			console.log("当前游戏在"+curRunGame);
			return false;
		}
		return true;
	}

	StartReConnect () {
		if (this.bStartReConnect)
			return;
		this.bStartReConnect = true;
		// this.reConnectUseTime = 0;
		// this.reConnectLable.string = '连接失败,重新加载中(1秒)...';
		// this.lastReConnectTime = new Date().getTime();
		// this.reConnectTip.active = true;
	}
	CloseReConnectTip () {
		this.bStartReConnect = false;
		this.lastReConnectTime = 0;
		this.reConnectUseTime = 0;
		this.reConnectLable.string = '';
		this.reConnectTip.active = false;
	}
	PushGlobalEffectNode (effectNode) {
		if (!this.node) {
			//节点已经被销毁了
			return;
		}
		effectNode.removeFromParent(false);
		this.node.getChildByName('globalEffect').addChild(effectNode);
	}

	GetIsFirstLogin () {
		return this.isFirstLogin
	}

	SetFirstLogin () {
		this.isFirstLogin = false;
	}

	//获取客户端启动配置
	GetClientConfig () {
		let clientConfig = [];
		let mt = ["GateServerIP", "GameServerIP", "AccountServerIP", "OrderServerIP", "ResServerIP"];
		for (var key in this.clientConfig) {
			if (mt.InArray(key)) {
				clientConfig[key] = this.MultiPoint(this.clientConfig[key]);
			} else {
				clientConfig[key] = this.clientConfig[key];
			}
		}
		return clientConfig;
	}
}
